package com.treeroot.userservice.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treeroot.commonutils.exceptionhandler.MyException;
import com.treeroot.commonutils.ordervo.UserInfoOrder;
import com.treeroot.commonutils.utils.JwtUtils;
import com.treeroot.userservice.user.entity.PcUserInfo;
import com.treeroot.userservice.user.entity.vo.RegisterVo;
import com.treeroot.userservice.user.mapper.PcUserInfoMapper;
import com.treeroot.userservice.user.service.PcUserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.treeroot.commonutils.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TreeRoot
 * @since 2022-04-28
 */
@Service
@Transactional
public class PcUserInfoServiceImpl extends ServiceImpl<PcUserInfoMapper, PcUserInfo> implements PcUserInfoService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 远程  根据用户id获取用户信息
     * @param id 用户id
     * @return
     */

    @Override
    public UserInfoOrder getUserOrder(String id) {
        PcUserInfo user = baseMapper.selectById(id);
        UserInfoOrder userInfoOrder = new UserInfoOrder();
        BeanUtils.copyProperties(user, userInfoOrder);
        return userInfoOrder;
    }

    /**
     * 条件查询带分页的方法
     * @param current 当前页
     * @param limit   最大页
     * @param user    需要的数据
     *
     */
    @Override
    public Map getQueryUserList(long current, long limit, PcUserInfo user) {
        Page<PcUserInfo> userInfoPage =new Page<>(current,limit);
        LambdaQueryWrapper<PcUserInfo> wrapper = new QueryWrapper<PcUserInfo>().lambda()

                .eq(
                        !StringUtils.isEmpty(user.getName()),
                        PcUserInfo::getName,
                        user.getName()
                ).eq(
                        !StringUtils.isEmpty(user.getPhone()),
                        PcUserInfo::getPhone,
                        user.getPhone()
                ).eq(
                        !StringUtils.isEmpty(user.getSsn()),
                        PcUserInfo::getSsn,
                        user.getSsn()
                );


        //    调用方法实现查询
        baseMapper.selectPage(userInfoPage, wrapper);
        long total = userInfoPage.getTotal();
        List<PcUserInfo> records = userInfoPage.getRecords();
        Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return map;
    }



    /**
     * 用户登录
     *
     * @param userInfo 用户实体类 信息
     *
     */
    @Override
    public String login(PcUserInfo userInfo) {
        String phone = userInfo.getPhone();
        String password = userInfo.getPassword();
        //校验参数
        if (StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(password)) {
            throw new MyException(20000, "请先填写手机号，密码");
        }

        // 判断手机号是否正确
        QueryWrapper<PcUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        PcUserInfo pcUserInfo = baseMapper.selectOne(wrapper);

        //判断查出来的对象是否为空
        if (pcUserInfo == null) {
            //无该手机号
            throw new MyException(20000, "该号码尚未注册");
        }

        //判断密码
        //因为存烤到数据库密码肯定加密的
        //把新入的密码进行加密，再和数据库密码进行比较
        //MD加密 32位
        if (!MD5.encrypt(password).equals(pcUserInfo.getPassword())) {

            throw new MyException(20000, "密码错误");
        }

        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(pcUserInfo.getId(), pcUserInfo.getUsername());

        return jwtToken;
    }



    /**
     * 用户注册
     *
     * @param registerVo  用户实体类 信息
     *
     */
    @Override
    public void register(RegisterVo registerVo) {
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        if (StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(code) ||
                StringUtils.isEmpty(password)) {
            throw new MyException(20000, "请先填写完整手机号，密码,验证码");
        }
        //获取redis验证码
//                判断验证码
        String redisCode = redisTemplate.opsForValue().get(phone);
        if (!code.equals(redisCode)) {
            throw new MyException(20000, "验证码不一致");

        }

        //       對断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<PcUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        PcUserInfo pcUserInfo = baseMapper.selectOne(wrapper);

        //判断查出来的对象是否为空
        if (!(pcUserInfo == null)) {

            //已有手机号
            throw new MyException(20000, "该号码已注册");
        }

        //        数据添加数据库中
        PcUserInfo newUserInfo = new PcUserInfo();
        newUserInfo.setPhone(phone);
        //        MD加密 32位
        newUserInfo.setPassword(MD5.encrypt(password));
        baseMapper.insert(newUserInfo);
    }


}
